import cv2 as cv
from scipy import ndimage
from skimage import io
import sys
import time
import os
import numpy as np


def progress_bar(i):
    print("\r", end="")
    print("处理进度: {}%:".format(i), "▋" * (i // 2), end="")
    sys.stdout.flush()
    time.sleep(0.05)


def check(M_list):
    n = []
    for i in M_list:
        if i not in n:
            n.append(i)
    return n


def create_file(out_folder):
    data_path = os.path.join(out_folder)  # 文件夹路径'out_folder'
    if not os.path.exists(data_path):  # 判断文件夹是否存在
        os.makedirs(data_path)  # 不存在则新建文件夹


def expand(M_image, M_list, out_folder):
    """expand(f,M_list,out_folder) 文档:
    ----------------------
            f :   需要处理的图像文件名
        M_list :  一个字符串列表，包含需要进行的所有的变换:
    out_folder :  输出文件夹名
    ----------------------
    M_list 可用变换包括(重复录入无效):
    - "10","-10","30","-30","90","-90" -> 整数表示(视觉上)顺时针旋转，负数反之
    - "lr","ud","ct" -> 分别表示左右翻转(绕y轴), 上下翻转(绕x轴)，绕圆心翻转
    - "sh30","sh-45" -> 分别表示剪切的操作，角度为与y轴的夹角，正值表示前倾/，负值则表示后仰
    - "T" -> 转置"""
    for i in range(0, 101):
        progress_bar(i)
    img = cv.imread(M_image, 0)
    h, w = img.shape
    check(M_list)
    create_file(out_folder)
    p = os.path.abspath(out_folder)
    for j in M_list:
        if j == "10":
            img2 = ndimage.rotate(img, -10, reshape=True)
            io.imsave(os.path.join(p, 'dog_[10].jpg'), img2)
        if j == "-10":
            img3 = ndimage.rotate(img, 10, reshape=True)
            io.imsave(os.path.join(p, 'dog_[-10].jpg'), img3)
        if j == "30":
            img4 = ndimage.rotate(img, -30, reshape=True)
            io.imsave(os.path.join(p, 'dog_[30].jpg'), img4)
        if j == "-30":
            img5 = ndimage.rotate(img, 30, reshape=True)
            io.imsave(os.path.join(p, 'dog_[-30].jpg'), img5)
        if j == "90":
            img6 = ndimage.rotate(img, -90, reshape=True)
            io.imsave(os.path.join(p, 'dog_[90].jpg'), img6)
        if j == "-90":
            img7 = ndimage.rotate(img, 90, reshape=True)
            io.imsave(os.path.join(p, 'dog_[-90].jpg'), img7)
        if j == "lr":
            img8 = np.fliplr(img)
            io.imsave(os.path.join(p, 'dog_[lr].jpg'), img8)
        if j == "ud":
            img9 = np.flipud(img)
            io.imsave(os.path.join(p, 'dog_[ud].jpg'), img9)
        if j == "ct":
            img10 = np.fliplr(np.flipud(img))
            io.imsave(os.path.join(p, 'dog_[ct].jpg'), img10)
        if j == "sh30":
            m = np.float32([[2, -1, h],
                            [0, 2, 0]])
            img11 = cv.resize(cv.warpAffine(img, m, ((2 * w + h), 2 * h)), (int((2 * w + h) * 0.5), h),
                              interpolation=cv.INTER_NEAREST)
            io.imsave(os.path.join(p, 'dog_[sh30].jpg'), img11)
        if j == "sh-45":
            m = np.float32([[1, -1, h], [0, 1, 0]])  # 获得变换矩阵(线性变换+位移)
            img12 = cv.warpAffine(img, m, (w + h, h))
            io.imsave(os.path.join(p, 'dog_[sh-45].jpg'), img12)
        if j == "T":
            img13 = np.transpose(img, axes=(1, 0))
            io.imsave(os.path.join(p, 'dog_[T].jpg'), img13)
    print(end='\n')
    print("完成进行的操作", check(M_list))
    print("所有的输出文件都被保存在", p)
